package com.itheima.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.dto.SmsCouponParam;
import com.itheima.excption.MyException;
import com.itheima.pojo.SmsCoupon;
import com.itheima.pojo.SmsCouponProductCategoryRelation;
import com.itheima.pojo.SmsCouponProductRelation;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class SmsCouponService {
    @DubboReference
    private SmsCouponApi smsCouponApi;
    @DubboReference
    private SmsCouponProductRelationApi smsCouponProductRelationApi;
    @DubboReference
    private SmsCouponProductCategoryRelationApi smsCouponProductCategoryRelationApi;

    /**
     * 添加优惠券 需要判断优惠券是否已存在
     * @param couponParam 封装的请求体
     * @return
     */
    public void create(SmsCouponParam couponParam) {
        //获取到所有的smsCoupon的name,判断是否相同
        boolean flag=smsCouponApi.isExist(couponParam);
        if(!flag){
            throw new MyException("优惠券重复无法添加");
        }
        SmsCoupon smsCoupon=new SmsCoupon();
        BeanUtils.copyProperties(couponParam,smsCoupon);
        smsCouponApi.create(smsCoupon);
        smsCouponProductRelationApi.create(couponParam.getProductRelationList());
        smsCouponProductCategoryRelationApi.create(couponParam.getProductCategoryRelationList());
    }

    /**
     * 删除优惠券 如果优惠券没有过期则不能删除
     * @param id
     */
    public void deleteById(Integer id) {
        //删除成功返回true
        boolean b = smsCouponApi.deleteById(id);
        //false表示删除不成功,说明未过期
        if(!b){
            throw new MyException("优惠券未过期");
        }
        //删除成功,也删除关联的优惠券信息
        smsCouponProductRelationApi.deleteByCouponId(id);
        smsCouponProductCategoryRelationApi.deleteByCouponId(id);
    }

    /**
     *修改优惠券 需要判断修改后是否和其他重名
     * @param id
     * @param smsCouponParam
     */
    public void updateById(Integer id, SmsCouponParam smsCouponParam) {

        //当前id查询name和smsCouponParam比对
        SmsCoupon sms = smsCouponApi.getSmsCouponById(id);
        if(sms.getName().equals(smsCouponParam.getName())){
            List<SmsCouponProductRelation> productRelationList = smsCouponParam.getProductRelationList();
            List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponParam.getProductCategoryRelationList();
            SmsCoupon smsCoupon=new SmsCoupon();
            BeanUtils.copyProperties(smsCouponParam,smsCoupon);
            smsCouponApi.updateById(smsCoupon);

            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelationApi.updateById(smsCouponProductRelation);
            }
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelationApi.updateById(smsCouponProductCategoryRelation);
            }
            return;
        }
        //和自己比对后,和其他进行比对
        //
        boolean flag=smsCouponApi.isExist(smsCouponParam);
        if(!flag){
            throw new MyException("优惠券名重复,无法添加");
        }else {
            List<SmsCouponProductRelation> productRelationList = smsCouponParam.getProductRelationList();
            List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponParam.getProductCategoryRelationList();
            SmsCoupon smsCoupon=new SmsCoupon();
            BeanUtils.copyProperties(smsCouponParam,smsCoupon);
            smsCouponApi.updateById(smsCoupon);

            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelationApi.updateById(smsCouponProductRelation);
            }
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelationApi.updateById(smsCouponProductCategoryRelation);
            }
        }




    }
    /**
     * 根据优惠券名称和类型分页获取优惠券列表,,模糊查询  分页
     * @param name
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResultVo<SmsCoupon> list(String name, Integer type, Integer pageNum, Integer pageSize) {
        IPage<SmsCoupon> iPage=smsCouponApi.list(name,type,pageNum,pageSize);
        PageResultVo<SmsCoupon> pageResultVo=new PageResultVo<>();
        pageResultVo.setList(iPage.getRecords());
        pageResultVo.setPageNum(pageNum);
        pageResultVo.setPageSize(pageSize);
        pageResultVo.setTotal(Convert.toInt(iPage.getTotal()));
        pageResultVo.setTotalPages(Convert.toInt(iPage.getPages()));
        return pageResultVo;
    }

    /**
     * 获取单个优惠券详细信息
     * @param id
     * @return
     */
    public SmsCouponParam getSmsCouponById(Integer id) {
        SmsCoupon smsCoupon=smsCouponApi.getSmsCouponById(id);
        List<SmsCouponProductRelation> productRelationList=smsCouponProductRelationApi.getSmsCouponById(id);
        List<SmsCouponProductCategoryRelation> productCategoryRelationList= smsCouponProductCategoryRelationApi.getSmsCouponById(id);
        SmsCouponParam smsCouponParam=new SmsCouponParam();
        BeanUtils.copyProperties(smsCoupon,smsCouponParam);
        smsCouponParam.setProductRelationList(productRelationList);
        smsCouponParam.setProductCategoryRelationList(productCategoryRelationList);
        return smsCouponParam;
    }
}
